<?php

namespace App\backend\controllers\admin;


use App\backend\controllers\AdminBaseController;
use App\backend\models\AdminUsersModel;
use App\backend\services\AdminUsersServices;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;

class AdminUsersController extends AdminBaseController
{
    protected $class_name = 'AdminUsers';

    /**
     * 操作员列表
     *
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function lists(Request $request)
    {
        $data = $request->all();
        $list = AdminUsersServices::getList($data);
        return successJson('获取成功!', $list);
    }

    /**
     * 获取 - 操作员详情
     *
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function detail(Request $request){
        $data = $request->all();
        $validator = Validator::make(
            $data,
            AdminUsersModel::update_rules(),
            AdminUsersModel::message()
        );
        if ($validator->fails()) {
            $msg = $validator->errors()->all();
            return errorJson($msg[0], []);
        }
        $list = AdminUsersServices::getDetail($data);
        return successJson('操作成功！', $list);
    }

    /**
     * 添加/编辑操作员
     */
    public function modify(Request $request)
    {

        $data = $request->all();
        $ruleArr = AdminUsersModel::modify_rules();
        if (!empty($data['id'])) {
            unset($ruleArr['password']);
            unset($ruleArr['confirm_password']);
            unset($data['password']);
            unset($data['confirm_password']);
        }
        $validator = Validator::make(
            $data,
            $ruleArr,
            AdminUsersModel::message()
        );
        if ($validator->fails()) {
            $msg = $validator->errors()->all();
            return errorJson($msg[0], []);
        }
        $list = AdminUsersServices::modify($data);
        if (!$list) {
            return errorJson('操作失败！', []);
        }
        return successJson('操作成功！', []);
    }

    /**
     * 删除操作员
     */
    public function delete(Request $request)
    {
        $data = $request->all();
        $validator = Validator::make(
            $data,
            AdminUsersModel::update_rules(),
            AdminUsersModel::message()
        );
        if ($validator->fails()) {
            $msg = $validator->errors()->all();
            return errorJson($msg[0], []);
        }
        $list = AdminUsersServices::delete($data);
        return successJson('操作成功！', $list);
    }


    /**
     * 改变审核状态
     */
    public function updateStatus(Request $request)
    {
        $data = $request->all();
        $validator = Validator::make(
            $data,
            AdminUsersModel::update_rules(),
            AdminUsersModel::message()
        );
        if ($validator->fails()) {
            $msg = $validator->errors()->all();
            return errorJson($msg[0], []);
        }
        $list = AdminUsersServices::updateStatus($data);
        return successJson('操作成功！', $list);
    }

}
